import axios from "axios";
import qs from "qs";
import { message } from "ant-design-vue";
import { ref } from "vue";
//后端地址，注意修改
let baseUrl = "http://ys-tongji.yyzhu.vip";
export async function getList(): Promise<any[]> {
    let { data } = await axios.post(
        baseUrl + "/api/getUrlById",
        qs.stringify({
            cookis: localStorage.getItem("z-cookis"),
            uid: localStorage.getItem("z-uid"),
        })
    ).catch((e)=>{
        message.error("出错了，这有可能是您的网络问题");
        return Promise.reject(e);
    })
    let url = "";
    if (data.code == 200) {
        url = data.data;
    } else {
        message.error(data.msg);
        return Promise.reject("");
    }

    let types = [
        {
            code: 100,
            label: "新手祈愿",
        },
        {
            code: 200,
            label: "常驻祈愿",
        },
        {
            code: 301,
            label: "活动祈愿",
        },
        {
            code: 302,
            label: "武器祈愿",
        },
    ];
    let obj = {} as any;
    let key = "asdsadas";
    const content = ref("Loading...");
    message.loading({ content: () => content.value, key, duration: 0 });
    for (let i = 0; i < types.length; i++) {
        const element = types[i];
        let list = [] as {}[];
        let end_id = "0";
        let num = 1;
        while (true) {
            content.value = `正在获取${element.label}第${num}页`;
            let temp = await getListByEnd(url, end_id, element.code);
            if (temp.length == 0) {
                break;
            }
            list = [...list, ...temp];
            end_id = temp[temp.length - 1].id;
            await sleep(500);
            num++;
        }
        obj[element.label] = list;
    }
    message.success({ content: "获取完成!", key, duration: 2 });
    return obj;
}

async function getListByEnd(url: any, end_id: any, gacha_type: any) {
    let { data } = await axios.post(
        baseUrl + "/api/getListByEnd",
        qs.stringify({
            url,
            end_id,
            gacha_type,
        })
    ).catch((e)=>{
        message.error("出错了，这有可能是您的网络问题");
        return Promise.reject(e);
    })
    if (data.code == 200) {
        return data.data;
    } else {
        message.error(data.msg);
        return Promise.reject("");
    }
}

async function sleep(delay = 1000) {
    return new Promise((resolve) => {
        setTimeout(() => {
            resolve("");
        }, delay);
    });
}
